﻿using System;
using System.Data.SqlClient;
using Devv.Core.Crypto;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Enums;

namespace Yglu.BusinessLayer.Entities
{
	public class User : BaseEntity
	{
		#region Properties

		public String PasswordHash { get; set; }
		public String FullName { get; set; }
		public String Phone { get; set; }
		public AdminLevel AdminLevel { get; set; }
		public Store Store { get; set; }

		#endregion

		#region Extra properties

		public String StoreTitle
		{
			get
			{
				return Store.Title;
			}
		}

		#endregion

		#region Constructor

		public User()
		{
		}

		public User(Int32 id)
		{
			Construct(DataAccess.User_Get(id));
		}

		public User(Int32 id, String password)
		{
			Construct(DataAccess.User_Get(id, password));
		}

		public User(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Methods

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "UserId");
			FullName = DataHandler.GetString(reader, "UserFullName");
			Phone = DataHandler.GetString(reader, "UserPhone");
			AdminLevel = (AdminLevel)DataHandler.GetInt32(reader, "UserAdminLevel");
			Active = DataHandler.GetBoolean(reader, "UserActive");

			Store = new Store();
			Store.Load(reader);
		}

		public static String GetPasswordHash(Int32 storeId, String password)
		{
			return Hash.SHA1(storeId + "-" + password);
		}

		#endregion
	}
}